<template>
    <el-form ref="formRef" style="max-width: 600px" :model="formDate" :rules="rules" label-width="auto"
        class="demo-ruleForm" status-icon>
        <el-form-item label="类目的名称" prop="name">
            <el-input v-model="formDate.name" />
        </el-form-item>
        <el-form-item label="类目图片" prop="img">
            <el-input v-model="formDate.img" />
            <Tuku  v-model="formDate.img" ></Tuku>
        </el-form-item>
        <el-form-item label="类目排序" prop="seq">
            <!-- <el-input v-model="formDate.seq" /> -->
            <el-input-number v-model="formDate.seq" :min="1" :max="10" />
        </el-form-item>
        <el-form-item label="父目录ID" prop="parentId">
            <el-tree-select v-model="formDate.parentId" 
                    :data="categorydata" 
                    check-strictly 
                    :render-after-expand="false" 
                    style="width: 180px" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm(formRef)">
                提交
            </el-button>
            <el-button @click="resetForm(formRef)">重置</el-button>
        </el-form-item>
    </el-form>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { CategoryApi } from '@/api';
import Tuku from '@/components/Tuku.vue';
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router';
let router=useRouter()

const formRef = ref()

const formDate = reactive({
    name: '',
    img: '',
    seq: 0,
    parentId: 0
})
const rules = reactive({
    name: [
        { required: true, message: '请输入您的姓名', trigger: 'blur' },
        { min: 1, max: 5, message: '最少长度是 1 to 5', trigger: 'blur' },
    ]
})
const categorydata = ref([
    {
        value: 0,
        label: '一级目录',
       
    },
])

const formSubmit = () => { 
    CategoryApi.insert.call({
        name: formDate.name,
        img: formDate.img,
        seq: formDate.seq,
        parentId:formDate.parentId
    }).then((res: any) => { 
        console.log(res);
        ElMessage.success("新增成功")
        //新增完成之后需要到哪里去
        router.push({name:'category-list'})
    })
}

onMounted(() => { 
    let eat = ["黄焖鸡", "鸭腿饭", "京酱面", "布袋馍"]
    const eatFooter = eat.reduce((duzi: any, currentValue: any) => {
        if (currentValue === "黄焖鸡") { duzi.push(currentValue) }
        if (currentValue === "鸭腿饭") { duzi.push(currentValue) }


        return duzi;
    }, [])
    console.log(eatFooter);
    CallcategoryApi()
})

//查询 获取categoryapi
const CallcategoryApi = () => { 
    CategoryApi.select.call().then((res:any) => { 
      
        res.forEach((item:any) => {
            item.value = item.id
            item.label = item.name
        });
     //console.log(res);
        const cateGoryData2 = res.reduce((hezi: any, currentValue: any) => {
            //console.log(currentValue);
            
            currentValue.children = res.filter((item: any) => { 
                return item.parentId === currentValue.id
            })
            if (currentValue.parentId === 0) { 
                hezi.push(currentValue)
            }
            return hezi;
         },[])
         console.log(cateGoryData2);
         categorydata.value=cateGoryData2
    })


}



const submitForm = async (formEl: any) => {
    if (!formEl) return
    await formEl.validate((valid: any, fields: any) => {
        if (valid) {
            console.log('submit!')
            formSubmit()
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: any) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>